﻿using CommonServiceLocator;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using RemoteTool.Business.RemoteManager;
using RemoteTool.Data;
using RemoteTool.Data.Model;
using WindowsInput;
using System.Threading.Tasks;

namespace RemoteTool.Business
{
    /// <summary>
    /// 向日葵桌面远程，13版本后界面有变化
    /// </summary>
    //[RemoteTool(ToolCode = "OSL", Memo = "向日葵桌面远程,最高支持10版，暂不支持11版本", ToolName = "向日葵桌面远程")]
    public class OraySunLoginDesktop : RemoteToolBase
    {
        /// <summary>
        /// 打开远程
        /// </summary>
        /// <param name="info"></param>
        public override Task OpenAsync(RemoteInfo info)
        {
            return OpenAsync(info, info.ToolCode == "OSL");
        }
        /// <summary>
        /// 打开远程
        /// </summary>
        /// <param name="info"></param>
        /// <param name="isDesktop"></param>
        protected Task OpenAsync(RemoteInfo info, Boolean isDesktop)
        {
            //String exe = GetExePath("向日葵");

            var cfgManager = ServiceLocator.Current.GetInstance<ConfigManager>();
            var exe = cfgManager.SysConfig.OraySunExePath;
            if (String.IsNullOrEmpty(exe))
            {
                throw new Exception("工具 向日葵 未找到，请确保正确安装，启动已中止");
            }
            String id = info.ID ?? "";
            // 去除ID里的空格
            id = id.Replace(" ", "");
            String pwd = (info.Password ?? "").Replace(" ", "");
            exe = Path.Combine(exe, "SunloginClient.exe");

            return Task.Run(() => {
                if (File.Exists(exe))
                {
                    var versionInfo = FileVersionInfo.GetVersionInfo(exe);
                    if (versionInfo.ProductVersion.StartsWith("10"))
                    {
                        var process = Process.Start(exe, $"--mod=fastcontrol --fastcode={id} --pwd={pwd}");
                        process.WaitForExit();
                        Console.WriteLine(process.ExitCode);
                    }
                    else if (versionInfo.ProductVersion.StartsWith("13"))
                    {
                        InputSimulator input = new InputSimulator();

                        var process = Process.Start(exe);
                        Console.WriteLine($"{DateTime.Now}开始等待退出**********************************");
                        process.WaitForExit(2000);
                        var code = process.HasExited ? process.ExitCode.ToString() : "not exited";
                        Console.WriteLine($"{DateTime.Now}退出完成->{code}");
                      
                        input.Keyboard.Sleep(1000);
                        input.Keyboard.TextEntry(id);
                        Console.WriteLine($"{DateTime.Now}开始发送tab键");
                        input.Keyboard.Sleep(200);
                        input.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.TAB);
                        input.Keyboard.Sleep(200);
                        for (int i = 0; i < 8; i++)
                        {
                            input.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.BACK);
                        }
                        input.Keyboard.TextEntry(pwd);
                        Console.WriteLine($"{DateTime.Now}开始发送enter键");
                        input.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN);

                    }
                    else
                    {
                        InputSimulator input = new InputSimulator();

                        var process = Process.Start(exe);
                        Console.WriteLine($"{DateTime.Now}开始等待退出**********************************");
                        process.WaitForExit(2000);
                        var code = process.HasExited ? process.ExitCode.ToString() : "not exited";
                        Console.WriteLine($"{DateTime.Now}退出完成->{code}");

                        input.Keyboard.Sleep(1000);
                        input.Keyboard.TextEntry(id);
                        Console.WriteLine($"{DateTime.Now}开始发送tab键");
                        input.Keyboard.Sleep(200);

                        input.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.TAB);
                        input.Keyboard.Sleep(200);
                        input.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.TAB);
                        input.Keyboard.Sleep(200);
                        for (int i = 0; i < 8; i++)
                        {
                            input.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.BACK);
                        }
                        input.Keyboard.TextEntry(pwd);
                        Console.WriteLine($"{DateTime.Now}开始发送enter键");
                        input.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN);

                    }
                }
            });
           
        }

    }
}
